/*
 * @(#)XfslConfig.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsl.model;

import xfuzzy.lang.*;
import java.io.*;

/**
 * Configuraci�n de un proceso de ajuste param�trico
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfslConfig {

	//----------------------------------------------------------------------------//
	//                            COSTANTES P�BLICAS                              //
	//----------------------------------------------------------------------------//

	public static final int NOT_SELECTED = -1;
	public static final int CANCEL = 0;
	public static final int UNSET = 1;
	public static final int SET = 2;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Fichero de entrenamiento
	 */
	public File trainingfile;
	
	/**
	 * Fichero de test
	 */
	public File testfile;
	
	/**
	 * Fichero de salida
	 */
	public File outputfile;
	
	/**
	 * Almacenamiento de la evoluci�n del proceso de ajuste
	 */
	public XfslLog logfile;
	
	/**
	 * Algoritmo de ajuste
	 */
	public XfslAlgorithm algorithm;
	
	/**
	 * Funci�n de error a optimizar
	 */
	public XfslErrorFunction errorfunction;
	
	/**
	 * Condiciones de parada
	 */
	public XfslEndCondition endcondition;
	
	/**
	 * Flag de modificaci�n
	 */
	public boolean modified = false;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Selecci�n de par�metros a ajustar
	 */
	private XfslSetting[] setting;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfslConfig() {
		trainingfile = null;
		testfile = null;
		outputfile = null;
		logfile = new XfslLog();
		algorithm = null;
		try { errorfunction = new XfslErrorFunction(0); } catch(XflException e) {}
		endcondition = new XfslEndCondition();
		setting = new XfslSetting[0];
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Representaci�n en formato XML
	 */
	public String toXML(String sysname) {
		String eol = System.getProperty("line.separator", "\n");
		String code = "<xfuzzy>"+eol; 
		code += "\t<xfsl system=\""+sysname+"\">"+eol;
		if(trainingfile != null)
			code += "\t\t<training file=\""+trainingfile.getAbsolutePath()+"\"></training>"+eol;
		if(testfile != null)
			code += "\t\t<test file=\""+testfile.getAbsolutePath()+"\"></test>"+eol;
		if(logfile.isOn()) code += logfile.toXML();
		if(outputfile != null)
			code += "\t\t<output file=\""+outputfile.getAbsolutePath()+"\"></output>"+eol;
		if(algorithm != null) code += algorithm.toXML();
		code += errorfunction.toXML();
		code += endcondition.toXML();
		for(int i=0; i<setting.length; i++) code += setting[i].toXML();
		code += "\t</xfsl>"+eol;
		code += "</xfuzzy>"+eol;
		return code;
	}

	/**
	 * Almacenar la configuracion en un fichero
	 */
	public boolean save(File file,String sysname, boolean append) {
		String code = toXML(sysname);
		byte buf[] = code.getBytes();

		try {
			OutputStream stream = new FileOutputStream(file,append);
			stream.write(buf);
			stream.close();
		}
		catch (IOException e) { return false; }
		return true;
	}

	/**
	 * Verifica que la configuraci�n puede ejecutarse
	 */
	public boolean isReadyToRun() {
		return (algorithm != null && trainingfile != null);
	}

	/**
	 * Obtiene el conjunto de selecciones de parametros	
	 */
	public XfslSetting[] getSettings() {
		return this.setting;
	}

	/**
	 * Asigna el conjunto de selecciones de parametros
	 */
	public void setSettings(XfslSetting[] stt) {
		this.setting = stt;
	}

	/**
	 * A�ade una selecci�n de parametros
	 */
	public void addSetting(String def, boolean enable) {
		XfslSetting[] aux = new XfslSetting[setting.length+1];
		System.arraycopy(setting,0,aux,0,setting.length);
		aux[setting.length] = new XfslSetting(def, enable);
		setting = aux;
	}

	/**
	 * Verifica si existe alguna selecci�n
	 */
	public boolean areSettingsOn() {
		return (setting.length>0);
	}

	/**
	 * Asigna las selecciones a los tipos de un sistema
	 */
	public void setParameterSettings(Type[] tp) {
		for(int i=0; i<setting.length; i++) setting[i].set(tp);
	}
}
